-- ========================================================================
-- nba2014-2015 database example queries
-- ========================================================================
-- The queries in the first four sections are the Neo4j Cypher queries from
-- the reference file, translated to CoraDB SELECT ... MATCH (PGQL). Cypher
-- CONTAINS is translated to LIKE '%...%', and the CALL {} subquery is
-- expressed with conditional aggregation. A final section adds league-wide
-- analytics.
-- ========================================================================

------------------------------------------------------------------------
-- Team, coach and roster lookups
------------------------------------------------------------------------

-- Retrieve coaches that graduated from Princeton.
SELECT n.name
FROM MATCH (n:coach)
WHERE n.college = 'Princeton';

-- Retrieve WEST conference teams information.
SELECT n.id, n.name, n.city, n.arena
FROM MATCH (n:team)
WHERE n.conference = 'WEST'
ORDER BY n.id ASC;

-- Find teams coached by Steve Kerr (with his win total).
SELECT c.name, t.name, r.wins
FROM MATCH (c:coach)-[r:coaches]->(t:team)
WHERE c.name = 'Steve Kerr';

-- Find the Sacramento Kings coaches, ordered by their tenure.
SELECT c.name, t.name, r.games, r.wins, r.loses
FROM MATCH (c:coach)-[r:coaches]->(t:team)
WHERE t.id = 'SAC'
ORDER BY r.`order` ASC;

------------------------------------------------------------------------
-- Games and matchups
------------------------------------------------------------------------

-- Golden State Warriors overtime games (opponent and date).
SELECT t1.id, t2.id, g.`date`
FROM MATCH (t1:team)-[:competes_in_game]->(g:game),
  (g:game)<-[:competes_in_game]-(t2:team)
WHERE g.overtimes = 1
AND t1.id = 'GSW'
AND t1 <> t2
ORDER BY g.`date` ASC;

-- Golden State Warriors overtime games (team and game details).
SELECT t1.name, t2.name, g.matchup, g.`date`, g.score
FROM MATCH (t1:team)-[:competes_in_game]->(g:game),
  (g:game)<-[:competes_in_game]-(t2:team)
WHERE g.overtimes = 1
AND t1.id = 'GSW'
AND t1 <> t2
ORDER BY g.`date` ASC;

-- View the players (from both teams) in the games played by Cleveland.
SELECT p1.name, p2.name, g.matchup, g.`date`
FROM MATCH
  (p1:player)-[:plays_for]->(t1:team),
  (t1:team)-[:competes_in_game]->(g:game),
  (g:game)<-[:competes_in_game]-(t2:team),
  (t2:team)<-[:plays_for]-(p2:player)
WHERE t2.id = 'CLE'
LIMIT 100;

-- View the coaches associated with games played by Cleveland.
SELECT c1.name, c2.name, t1.name, t2.name, g.matchup
FROM MATCH
  (c1:coach)-[:coaches]->(t1:team),
  (t1:team)-[:competes_in_game]->(g:game),
  (g:game)<-[:competes_in_game]-(t2:team),
  (t2:team)<-[:coaches]-(c2:coach)
WHERE g.matchup LIKE '%CLE%'
LIMIT 20;

-- View games between GSW and CLE where GSW was the winner. (t1 is pinned to GSW so
-- each game is listed once, not once from each side.)
SELECT t1.name, t2.name, g.matchup, g.`date`, g.score
FROM MATCH (t1:team)-[:competes_in_game]->(g:game),
  (g:game)<-[:competes_in_game]-(t2:team)
WHERE t1.id = 'GSW'
AND t2.id = 'CLE'
AND g.winner_team_id = 'GSW'
ORDER BY g.`date` ASC;

------------------------------------------------------------------------
-- Player performance and playoffs
------------------------------------------------------------------------

-- Players that played in the finals (conference and NBA finals).
SELECT DISTINCT p1.name, p2.name, s.round
FROM MATCH
  (p1:player)-[:plays_in_game]->(g1:game),
  (g1:game)-[:is_part_of_series]->(s:series),
  (s:series)<-[:is_part_of_series]-(g2:game),
  (g2:game)<-[:plays_in_game]-(p2:player)
WHERE s.round LIKE '%Finals%'
AND p1 <> p2
LIMIT 100;

-- All-Star players (pairs that played in the All-Star game).
SELECT p1.name, p2.name
FROM MATCH
  (p1:player)-[:plays_in_all_star_game]->(a:all_star_game),
  (a:all_star_game)<-[:plays_in_all_star_game]-(p2:player)
WHERE p1.id < p2.id;

-- Stephen Curry and LeBron James meetings in the finals.
SELECT DISTINCT p1.name, p2.name, s.round, g1.matchup
FROM MATCH
  (p1:player)-[:plays_in_game]->(g1:game),
  (g1:game)-[:is_part_of_series]->(s:series),
  (s:series)<-[:is_part_of_series]-(g2:game),
  (g2:game)<-[:plays_in_game]-(p2:player)
WHERE p1.name LIKE '%Curry%'
AND p2.name LIKE '%LeBron%';

-- Brooklyn-Miami games where Sergey Karasev played for Brooklyn.
SELECT p.name, t1.name, t2.name, g.matchup, g.`date`
FROM MATCH
  (p:player)-[:plays_for]->(t1:team),
  (t1:team)-[:competes_in_game]->(g:game),
  (g:game)<-[:competes_in_game]-(t2:team)
WHERE t1.city LIKE '%Brooklyn%'
AND t2.city LIKE '%Miami%'
AND p.name = 'Sergey Karasev';

-- Boston-Miami games where players taller than 6.12 feet played for Boston.
SELECT DISTINCT t1.name, t2.name, g.matchup, g.`date`
FROM MATCH
  (p:player)-[:plays_for]->(t1:team),
  (t1:team)-[:competes_in_game]->(g:game),
  (g:game)<-[:competes_in_game]-(t2:team)
WHERE t1.city LIKE '%Boston%'
AND t2.city LIKE '%Miami%'
AND p.height > 6.12;

-- Games played by Curry against EAST conference teams with more than 6 free throws made.
SELECT p.name, t2.name, g.matchup, g.`date`, r.free_throws
FROM MATCH (p:player)-[r:plays_in_game]->(g:game),
  (g:game)<-[:competes_in_game]-(t2:team)
WHERE t2.conference = 'EAST'
AND p.name = 'Stephen Curry'
AND r.free_throws > 6
ORDER BY g.`date` ASC;

-- Games played by Curry against EAST teams with >50% three-point accuracy where GSW lost.
SELECT p.name, t2.name, g.matchup, g.`date`
FROM MATCH (p:player)-[r:plays_in_game]->(g:game),
  (g:game)<-[:competes_in_game]-(t2:team)
WHERE t2.conference = 'EAST'
AND p.name = 'Stephen Curry'
AND r.three_points_attempts > 0
AND (r.three_points * 100) / r.three_points_attempts > 50
AND g.winner_team_id <> 'GSW'
ORDER BY g.`date` ASC;

-- Count of games GSW won in Curry vs LeBron matchups where Curry shot >45% from three
-- and LeBron shot <85% from the free-throw line.
SELECT COUNT(g) AS gamesWon
FROM MATCH (p1:player)-[r1:plays_in_game]->(g:game),
  (g:game)<-[r2:plays_in_game]-(p2:player)
WHERE p1.name = 'Stephen Curry'
AND p2.name = 'LeBron James'
AND g.winner_team_id = 'GSW'
AND r1.three_points_attempts > 0
AND r2.free_throws_attempts > 0
AND (r1.three_points * 100) / r1.three_points_attempts > 45
AND (r2.free_throws * 100) / r2.free_throws_attempts < 85;

-- GSW win/loss and win-percentage in those same Curry vs LeBron matchups.
SELECT
  SUM(CASE WHEN g.winner_team_id = 'GSW' THEN 1 ELSE 0 END) AS won,
  SUM(CASE WHEN g.winner_team_id = 'CLE' THEN 1 ELSE 0 END) AS lost,
  (SUM(CASE WHEN g.winner_team_id = 'GSW' THEN 1 ELSE 0 END) * 100) / COUNT(g) AS gsw_win_pct
FROM MATCH (p1:player)-[r1:plays_in_game]->(g:game),
  (g:game)<-[r2:plays_in_game]-(p2:player)
WHERE p1.name = 'Stephen Curry'
AND p2.name = 'LeBron James'
AND r1.three_points_attempts > 0
AND r2.free_throws_attempts > 0
AND (r1.three_points * 100) / r1.three_points_attempts > 45
AND (r2.free_throws * 100) / r2.free_throws_attempts < 85
AND g.winner_team_id IN ('GSW', 'CLE');

------------------------------------------------------------------------
-- League analytics (aggregate / business functions)
------------------------------------------------------------------------

-- Top 10 scorers by total points across the season.
SELECT p.name, SUM(r.points) AS totalPoints
FROM MATCH (p:player)-[r:plays_in_game]->(g:game)
GROUP BY p.name
ORDER BY totalPoints DESC
LIMIT 10;

-- Top 10 players by average points per game.
SELECT p.name, COUNT(g) AS gamesPlayed, AVG(r.points) AS pointsPerGame
FROM MATCH (p:player)-[r:plays_in_game]->(g:game)
GROUP BY p.name
ORDER BY pointsPerGame DESC
LIMIT 10;

-- Players who played for more than one team during the season.
SELECT p.name, COUNT(DISTINCT t.id) AS teams
FROM MATCH (p:player)-[:plays_for]->(t:team)
GROUP BY p.name
HAVING COUNT(DISTINCT t.id) > 1
ORDER BY teams DESC;

-- Coaches ranked by total wins.
SELECT c.name, SUM(r.wins) AS totalWins
FROM MATCH (c:coach)-[r:coaches]->(t:team)
GROUP BY c.name
ORDER BY totalWins DESC
LIMIT 10;

-- Referees ranked by number of games officiated.
SELECT rf.name, COUNT(g) AS games
FROM MATCH (rf:referee)-[:presides]->(g:game)
GROUP BY rf.name
ORDER BY games DESC
LIMIT 10;

-- How many games did each team compete in?
SELECT t.name, COUNT(g) AS games
FROM MATCH (t:team)-[:competes_in_game]->(g:game)
GROUP BY t.name
ORDER BY games DESC;

-- Number of players by nationality (top 10).
SELECT p.nationality, COUNT(p) AS players
FROM MATCH (p:player)
GROUP BY p.nationality
ORDER BY players DESC
LIMIT 10;


